package com.example.administrator.printoutindemo.fragment;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.adapter.JiageweihuAdapter;
import com.example.administrator.printoutindemo.base.BaseFragment;
import com.example.administrator.printoutindemo.bean.JiageweihuBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseConstants;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class JiageweihuListFragment extends BaseFragment {
    private View view;
    private TextView tv_biaotou, tv_rq, tv_chaxun, et_cx;
    private ListView lv_fklb;
    private JiageweihuAdapter mAdapter;
    private List<JiageweihuBean> mlists;
    private EditText et_sqrxm;
    private int year, monthOfYear, dayOfMonth;
    private AlertDialog alertDialog; //单选框

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_jiage, null);
        tv_biaotou = view.findViewById(R.id.tv_biaotou);
        tv_biaotou.setText("价格补充列表");
        tv_rq = view.findViewById(R.id.tv_rq);
        lv_fklb = view.findViewById(R.id.lv_fklb);
        mlists = new ArrayList<>();
        mAdapter = new JiageweihuAdapter(getContext(), mlists);
        lv_fklb.setAdapter(mAdapter);
        //获取系统时间
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        monthOfYear = calendar.get(Calendar.MONTH);
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        getCheckedDeliveryList();
        tv_rq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        if (month < 9) {
                            if (dayOfMonth < 10) {
                                tv_rq.setText(year + "-" + "0" + (month + 1) + "-" + "0" + dayOfMonth);
                            } else {
                                tv_rq.setText(year + "-" + "0" + (month + 1) + "-" + dayOfMonth);
                            }
                        } else {
                            if (dayOfMonth < 10) {
                                tv_rq.setText(year + "-" + (month + 1) + "-" + "0" + dayOfMonth);
                            } else {
                                tv_rq.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
                            }
                        }

                    }
                }, year, monthOfYear, dayOfMonth);
                datePickerDialog.show();

            }
        });
        tv_chaxun = view.findViewById(R.id.tv_chaxun);
        lv_fklb = view.findViewById(R.id.lv_fklb);
        et_sqrxm = view.findViewById(R.id.et_sqrxm);
        et_cx = view.findViewById(R.id.et_cx);
        et_cx.setText("4.2米货车");
        et_cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items = {"4.2米货车", "9.6米货车", "13米货车", "3吨叉车", "16吨吊车", "50吨吊车", "暂无信息"};
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
                alertBuilder.setTitle("车型");
                alertBuilder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getActivity(), items[i], Toast.LENGTH_SHORT).show();
                        et_cx.setText(items[i]);
                    }
                });
                alertBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        alertDialog.dismiss();
                    }
                });

                alertBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog = alertBuilder.create();
                alertDialog.show();
            }
        });
        tv_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getCheckedDeliveryList1();
            }
        });
        return view;
    }

    private void getCheckedDeliveryList() {


        String methodName = "GetCheckedDeliveryList";

        SoapParams params = new SoapParams();
        params.put("applicant", "");
        params.put("applyDate", "");
        params.put("models", "");

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    mAdapter.shuas();
                    Log.e("jiageweihulist========", result);
                    List<JiageweihuBean> inKuBean = new Gson().fromJson(result, new TypeToken<List<JiageweihuBean>>() {
                    }.getType());
                    mAdapter.addAll(inKuBean);
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();
            }
        }, getAapiUserName(), getApiPwd());
    }

    private void getCheckedDeliveryList1() {


        String methodName = "GetCheckedDeliveryList";

        SoapParams params = new SoapParams();
        params.put("applicant", et_sqrxm.getText().toString());
        params.put("applyDate", tv_rq.getText().toString());
        params.put("models", et_cx.getText().toString());

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    mAdapter.shuas();
                    List<JiageweihuBean> inKuBean = new Gson().fromJson(result, new TypeToken<List<JiageweihuBean>>() {
                    }.getType());
                    mAdapter.addAll(inKuBean);
                    tv_rq.setText("");
                    et_cx.setText("");
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();
            }
        }, getAapiUserName(), getApiPwd());
    }
}
